/*
 *  JFLAP - Formal Languages and Automata Package
 * 
 * 
 *  Susan H. Rodger
 *  Computer Science Department
 *  Duke University
 *  August 27, 2009

 *  Copyright (c) 2002-2009
 *  All rights reserved.

 *  JFLAP is open source software. Please see the LICENSE for terms.
 *
 */





package view.automata.simulate;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JToggleButton;

import model.algorithms.testinput.simulate.Configuration;

/**
 * This is a toggle button that displays a configuration inside of it.
 * 
 * @author Thomas Finley
 */

public class ConfigurationButton extends JToggleButton {

	private static final int PADDING = 5;
	public static final Color[] STATE_COLOR = { new Color(0, 0, 0, 0),
		new Color(0, 150, 0, 80), new Color(255, 0, 0, 80),
		new Color(100, 100, 255, 80), new Color(255, 255, 0, 80) };
	private int state = NORMAL;
	private Configuration config = null;
	private ConfigurationIcon icon = null;
	public static final int NORMAL = 0, ACCEPT = 1, REJECT = 2, FREEZE = 3;
	private static final String[] TEXT = { "Normal", "Accept", "Reject",
		"Freeze"};
	/**
	 * Instantiates a configuration button with a configuration with the default
	 * setting of the state being labeled as NORMAL.
	 * 
	 * @param config
	 *            the configuration to set the configuration button for
	 */
	public ConfigurationButton(Configuration config) {
		this(config, NORMAL);
	}

	/**
	 * Instantiates a configuration button with a configuration and its state.
	 * 
	 * @param config
	 *            the configuration to set the configuration button for
	 * @param state
	 *            the state to create the configuration button with, with states
	 *            as defined in the class <CODE>ConfigurationPane</CODE>
	 */
	public ConfigurationButton(Configuration config, int state) {
		super();
		this.config = config;
		icon = ConfigurationIconFactory.iconForConfiguration(config);
		// this.setIcon(icon);

		this.setHorizontalTextPosition(CENTER);
		this.setVerticalTextPosition(TOP);

		this.setState(state);
		Dimension size = new Dimension(0, PADDING * 2
				+ icon.getIconHeight());
		this.setPreferredSize(size);
		this.setMinimumSize(size);
	}

	/**
	 * Sets the state of this configuration button.
	 * 
	 * @param state
	 *            the new state, for example, <CODE>NORMAL</CODE>
	 */
	public void setState(int state) {
		// Is the correct state?
		if (config.isAccept())
			state = ACCEPT;
		if (state < 0 || state >= TEXT.length)
			state = NORMAL;
		this.state = state;
	}
	
	public int getState() {
		return state;
	}

	/**
	 * Returns the configuration represented by this configuration button.
	 * 
	 * @return the configuration represented by this configuration button
	 */
	public Configuration getConfiguration() {
		return config;
	}

	/**
	 * Paints this component.
	 * 
	 * @param g
	 *            the graphics object to paint this component to
	 */
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(STATE_COLOR[state]);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.translate(PADDING, PADDING);

		icon.paintConfiguration(this, (Graphics2D) g, getWidth() - PADDING * 2,
				getHeight() - PADDING * 2);
		g.translate(-PADDING, -PADDING);
	}

}
